// pages/clinicQuery/clinicQuery.js
const api = require("../../utils/api")
const util = require("../../utils/util")
const cache = require("../../utils/cache")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    dateStart: '',
    dateEnd: '',
    currentPatient: '',
    clinicList: [],
    examineList: []
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
    });
    this.handleQuery();
  },
  handleStartDateChange(e) {
    this.setData({
      dateStart: e.detail.value
    })
  },
  handleEndDateChange(e) {
    this.setData({
      dateEnd: e.detail.value
    })
  },
  handleClinicItemClick(event) {
    let applyNo = event.currentTarget.dataset.target
    let item = this.data.clinicList.find(m=> m.applyNo == applyNo)
    wx.navigateTo({
      url: '/pages/clinicQuery/clinicQueryDetail?applyNo='+item.applyNo+"&applyDeptname="+item.applyDeptname+"&execDeptname="+item.execDeptname+"&execTime="+item.execTime+"&applyItemName="+item.applyItemName,
    })
  },
  handleExamineItemClick(event) {
    let applyNo = event.currentTarget.dataset.target
    let item = this.data.examineList.find(m=> m.applyNo == applyNo)
    wx.navigateTo({
      url: '/pages/clinicQuery/examineQueryDetail?applyNo='+item.applyNo+"&applyDeptname="+item.applyDeptname+"&execDeptname="+item.execDeptname+"&execTime="+item.execTime+"&applyItemName="+item.applyItemName
    })
  },
  handleQuery(){
    //当前病人
    let cachePtList = cache.getValue("patient");
    if (cachePtList != undefined && cachePtList != null && cachePtList.length > 0) {
      cachePtList.forEach(m => {
        if (m.crtFlag == "Y") {
          this.data.currentPatient = m;
        }
      })
    }
    let params = {
      ioNo : this.data.currentPatient.ioNo,
      startDt : this.data.dateStart + " 00:00:00",
      endDt: this.data.dateEnd+ " 23:59:59",
      type: '1'
    }
    let that = this
    if(this.data.TabCur == 1){
      params.type = '2'
      api.getApply(params).then(res=>{
        that.data.examineList = res
        that.data.examineList.forEach(m=>{
          m.applyTime = util.timestampToDate(m.applyTime)
          m.execTime = util.timestampToTime(m.execTime)
        })
        that.setData({
          examineList: that.data.examineList
        })
      })
    } else{   
      params.type = '1'
      api.getApply(params).then(res=>{
        that.data.clinicList = res
        that.data.clinicList.forEach(m=>{
          m.applyTime = util.timestampToDate(m.applyTime)
          m.execTime = util.timestampToTime(m.execTime)
        })
        that.setData({
          clinicList: that.data.clinicList
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id != undefined) {
      this.setData({
        TabCur: options.id,
      })
    }
    let dateStart = new Date();
    let dateEnd = new Date();
//时间
    dateStart = dateStart.setDate(dateStart.getDate() - 7);
    dateStart = new Date(dateStart);
    this.data.dateStart = util.formatDate(dateStart)
    this.data.dateEnd = util.formatDate(dateEnd)
    this.setData({
      dateStart: this.data.dateStart,
      dateEnd: this.data.dateEnd
    })
    this.handleQuery();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})